<?php

namespace admin\modules\equipment_info\models;

use common\models\db\EquipmentAppVersionUpdateLog;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * EquipmentAppVersionUpdateLogSearch represents the model behind the search form about `admin\modules\equipment_info\models\EquipmentAppVersionUpdateLog`.
 */
class EquipmentAppVersionUpdateLogSearch extends EquipmentAppVersionUpdateLog
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'equipment_id', 'old_version_id', 'new_version_id', 'happen_from'], 'integer'],
            [['happen_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = EquipmentAppVersionUpdateLog::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'equipment_id' => $this->equipment_id,
            'old_version_id' => $this->old_version_id,
            'new_version_id' => $this->new_version_id,
            'happen_from' => $this->happen_from,
//            'happen_at' => $this->happen_at,
        ]);

        if ( ! is_null($this->happen_at) && strpos($this->happen_at, ' - ') !== false ) {
            list($start_date, $end_date) = explode(' - ', $this->happen_at);
            $query->andFilterWhere(['between', 'happen_at', strtotime($start_date), strtotime($end_date.' 23:59:59')]);
        }

        return $dataProvider;
    }
}
